package night_priority_queue;

import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import com.rabbitmq.client.MessageProperties;
import util.RabbitMqUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @BelongsProject: RabbitMQ
 * @BelongsPackage: workQueue
 * @Author: HUI
 * @CreateTime: 2022-04-13 09:48
 * @Description: 多线程式接收消息
 */
public class MyWorks {

    private static final String QUEUE_NAME = "work";

    public  void getWork() throws Exception {
        Channel channel = RabbitMqUtils.getChannel();

        //设置队列的最大优先级 最大可以设置到 255 官网推荐 1-10 如果设置太高比较吃内存和 CPU
        Map<String, Object> params = new HashMap();
        params.put("x-max-priority", 10);
        channel.queueDeclare(QUEUE_NAME, true, false, false, params);


        DeliverCallback deliverCallback = (consumerTag, delivery) -> {
            String receivedMessage = new String(delivery.getBody());
            System.out.println("接收到消息:" + receivedMessage);
        };

        CancelCallback cancelCallback = (consumerTag) -> {
            System.out.println(consumerTag + "消费者取消消费接口回调逻辑");
        };

        System.out.println("消费者启动等待消费.................. ");
        channel.basicConsume(QUEUE_NAME,true,deliverCallback,cancelCallback);

    }

    public static void main(String[] args) throws Exception {

      new MyWorks().getWork();

    }
}



